
# A Pile represents the pile of cards on the table during a card game
require 'observer'
require_relative 'card_list.rb'
require_relative 'poker_subject'

class Pile
  include PokerSubject
  # Create an empty hand.
	def initialize
    super
    @card_list = CardList.new
  end

  # Add card to pile.
  def add card
    @card_list.add card
  end

  # Empty the pile.
  def empty!
    @card_list.empty!
  end

  # Sort the pile.
  def sort!
    @card_list.sort
  end

  # Return pile as string
  def to_string
    @card_list.to_s
  end

  # iterate over all cards in pile
  def each
    @card_list.each{|card| yield card}
  end

  # Return number of cards in pile
  def no_of_cards
    @card_list.no_of_cards
  end

  # Return the best card in the pile
  def calculate_winner
    sorted_cards = @card_list.sort  # sort cards in 'poker' order
    result = sorted_cards[-1]                # return best card
    result
  end

  # Return true iff top two cards of same suit
  def snap?
    if @card_list.length < 2
      false
    # check for snap!
    elsif @card_list[-1].suit == @card_list[-2].suit
      true
    else
      false
    end
  end
end